package com.cutefool.zero.code.core.domain;

import com.cutefool.dsl.mybatis.core.objects.BasicsDomain;
import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Generated;
import java.sql.JDBCType;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;

@Data
@EqualsAndHashCode(callSuper = true)
public class DictionaryItem extends BasicsDomain<DictionaryItem> {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("NAME_")
    private String name;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("CODE_")
    private String code;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DEFINE_CODE_")
    private String defineCode;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DESCRIPTION_")
    private String description;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("FONT_COLOR_")
    private String fontColor;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("FONT_BG_COLOR_")
    private String fontBgColor;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("DISABLED_")
    private Byte disabled;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("ICON_")
    private String icon;

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @JsonAlias("COMPANY_ID_")
    private Long companyId;

    private static final DictionaryItem instance = new DictionaryItem();

    private static final Table table = new Table();

    public static DictionaryItem instance() {
        return instance;
    }

    @Override
    public Table table() {
        return table;
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<InsertDSL<DictionaryItem>> insert() {
        return (InsertDSL<DictionaryItem> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.code).toProperty("code");
            dsl.map(table.defineCode).toProperty("defineCode");
            dsl.map(table.description).toProperty("description");
            dsl.map(table.fontColor).toProperty("fontColor");
            dsl.map(table.fontBgColor).toProperty("fontBgColor");
            dsl.map(table.disabled).toProperty("disabled");
            dsl.map(table.icon).toProperty("icon");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    @SuppressWarnings("DuplicatedCode")
    public UnaryOperator<MultiRowInsertDSL<DictionaryItem>> insertMultiple() {
        return (MultiRowInsertDSL<DictionaryItem> dsl) -> {
            dsl.map(table.id).toProperty("id");
            dsl.map(table.name).toProperty("name");
            dsl.map(table.code).toProperty("code");
            dsl.map(table.defineCode).toProperty("defineCode");
            dsl.map(table.description).toProperty("description");
            dsl.map(table.fontColor).toProperty("fontColor");
            dsl.map(table.fontBgColor).toProperty("fontBgColor");
            dsl.map(table.disabled).toProperty("disabled");
            dsl.map(table.icon).toProperty("icon");
            dsl.map(table.createBy).toProperty("createBy");
            dsl.map(table.createAt).toProperty("createAt");
            dsl.map(table.updateBy).toProperty("updateBy");
            dsl.map(table.updateAt).toProperty("updateAt");
            dsl.map(table.companyId).toProperty("companyId");
            return dsl;
        };
    }

    @Override
    public void updateAllColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.name).equalTo(this::getName)
           .set(table.code).equalTo(this::getCode)
           .set(table.defineCode).equalTo(this::getDefineCode)
           .set(table.description).equalTo(this::getDescription)
           .set(table.fontColor).equalTo(this::getFontColor)
           .set(table.fontBgColor).equalTo(this::getFontBgColor)
           .set(table.disabled).equalTo(this::getDisabled)
           .set(table.icon).equalTo(this::getIcon)
           .set(table.createBy).equalTo(this::getCreateBy)
           .set(table.createAt).equalTo(this::getCreateAt)
           .set(table.updateBy).equalTo(this::getUpdateBy)
           .set(table.updateAt).equalTo(this::getUpdateAt)
           .set(table.companyId).equalTo(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    public void updateSelectiveColumns(UpdateDSL<UpdateModel> dsl) {
        dsl.set(table.name).equalToWhenPresent(this::getName)
           .set(table.code).equalToWhenPresent(this::getCode)
           .set(table.defineCode).equalToWhenPresent(this::getDefineCode)
           .set(table.description).equalToWhenPresent(this::getDescription)
           .set(table.fontColor).equalToWhenPresent(this::getFontColor)
           .set(table.fontBgColor).equalToWhenPresent(this::getFontBgColor)
           .set(table.disabled).equalToWhenPresent(this::getDisabled)
           .set(table.icon).equalToWhenPresent(this::getIcon)
           .set(table.createBy).equalToWhenPresent(this::getCreateBy)
           .set(table.createAt).equalToWhenPresent(this::getCreateAt)
           .set(table.updateBy).equalToWhenPresent(this::getUpdateBy)
           .set(table.updateAt).equalToWhenPresent(this::getUpdateAt)
           .set(table.companyId).equalToWhenPresent(this::getCompanyId);
        if(Objects.nonNull(getId())) {
            dsl.where(table.id, SqlBuilder.isEqualTo(this::getId));
        }
    }

    @Override
    @SuppressWarnings("all")
    public SqlColumn[] columns() {
        return new SqlColumn[]{table.id, table.name, table.code, table.defineCode, table.description, table.fontColor, table.fontBgColor, table.disabled, table.icon, table.createBy, table.createAt, table.updateBy, table.updateAt, table.companyId};
    }

    @Override
    public Map<String, SqlColumn<?>> columnsMap() {
        Map<String, SqlColumn<?>> maps = new LinkedHashMap<>(14);
        maps.put("id", table.id);
        maps.put("name", table.name);
        maps.put("code", table.code);
        maps.put("defineCode", table.defineCode);
        maps.put("description", table.description);
        maps.put("fontColor", table.fontColor);
        maps.put("fontBgColor", table.fontBgColor);
        maps.put("disabled", table.disabled);
        maps.put("icon", table.icon);
        maps.put("createBy", table.createBy);
        maps.put("createAt", table.createAt);
        maps.put("updateBy", table.updateBy);
        maps.put("updateAt", table.updateAt);
        maps.put("companyId", table.companyId);
        return maps;
    }

    @Override
    public SqlColumn<Long> id() {
        return table.id;
    }

    public static final class Table extends org.mybatis.dynamic.sql.AliasableSqlTable<Table> {
        public final SqlColumn<Long> id = column("ID_", JDBCType.BIGINT);

        public final SqlColumn<String> name = column("NAME_", JDBCType.VARCHAR);

        public final SqlColumn<String> code = column("CODE_", JDBCType.VARCHAR);

        public final SqlColumn<String> defineCode = column("DEFINE_CODE_", JDBCType.VARCHAR);

        public final SqlColumn<String> description = column("DESCRIPTION_", JDBCType.VARCHAR);

        public final SqlColumn<String> fontColor = column("FONT_COLOR_", JDBCType.VARCHAR);

        public final SqlColumn<String> fontBgColor = column("FONT_BG_COLOR_", JDBCType.VARCHAR);

        public final SqlColumn<Byte> disabled = column("DISABLED_", JDBCType.TINYINT);

        public final SqlColumn<String> icon = column("ICON_", JDBCType.VARCHAR);

        public final SqlColumn<Long> createBy = column("CREATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> createAt = column("CREATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> updateBy = column("UPDATE_BY_", JDBCType.BIGINT);

        public final SqlColumn<LocalDateTime> updateAt = column("UPDATE_AT_", JDBCType.TIMESTAMP);

        public final SqlColumn<Long> companyId = column("COMPANY_ID_", JDBCType.BIGINT);

        public Table() {
            super("DICTIONARY_ITEM", Table::new);
        }
    }
}